package com.cskaoyan.gateway.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.user.intercepter.TokenIntercepter;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author break_a_leg
 * @description
 * @date 2021年11月01日 16:03
 */
public class TokenUtil {

    public static Long getUserId(HttpServletRequest request) {
        // 获取token令牌
        Map userInfo = (Map) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        // 解析获取userId
//        JSONObject object = JSON.parseObject(userInfo);
        return Long.parseLong(userInfo.get("uid").toString());
    }

    public static String getUsername(HttpServletRequest request) {
        // 获取token令牌
        Map userInfo = (Map) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        // 解析获取userId
//        JSONObject object = JSON.parseObject(userInfo);
        return userInfo.get("username").toString();
    }
}
